
package ua.gradsoft.misc.jsoo.ant;

import java.util.Arrays;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.DirSet;
import org.apache.tools.ant.types.Path;
import ua.gradsoft.misc.jsoo.Configuration;
import ua.gradsoft.misc.jsoo.JSOOConfigurationException;
import ua.gradsoft.misc.jsoo.JSOOProcessingException;
import ua.gradsoft.misc.jsoo.Main;
import ua.gradsoft.termware.TermWare;

/**
 *
 * @author rssh
 */
public class JSOOTask extends Task
{

    /** Creates a new instance of JPETask */
    public JSOOTask() {
    }

    public void setJpehome(String value) {
        configuration_.setJPEHome(value);
    }

    public void setInput(String value) {
        configuration_.setInputDir(value);
    }

    public void  setOutput(String value) {
        configuration_.setOutputDir(value);
    }

    public void  setCreateOutputDir(boolean value) {
        configuration_.setCreateOutputDir(value);
    }


    public void addConfiguredIncludedirs(DirSet dirSet) {
        dirSet.setProject(this.getProject());
        configuration_.getIncludeDirs().add(dirSet.getDir(getProject()).getAbsolutePath());
    }

    public void addConfiguredIncludejars(Path path) {
        configuration_.getIncludeJars().addAll(Arrays.asList(path.list()));
    }


    public void setDebugLevel(int debugLevel)
    {
        configuration_.setDebugLevel(debugLevel);
    }

    public void setSilent(boolean silent)
    {
        configuration_.setSilent(silent);
    }


    public void setTransformation(String transformation) {
        configuration_.setTransformationName(transformation);
    }

    public void setFork(boolean fork) {
        fork_=fork; }

    public void setFailOnError(boolean value) {
        failOnError_=value;
    }


   public static class ClassnameHolder
    {
        private String classname_;

        public String getClassname()
        { return classname_; }
        public void setClassname(String classname)
        { classname_=classname; }
    }

    public void  addConfiguredDisable(ClassnameHolder value)
    {
        configuration_.addDisabledClass(value.getClassname());
    }

    public static class ClassnamepatternHolder
    {
         private String classnamepattern_;

        public String getClassnamepattern()
        { return classnamepattern_; }
        public void setClassnamepattern(String classnamepattern)
        { classnamepattern_=classnamepattern; }

    }

  public void execute() throws BuildException {
        if (failOnError_) {
            try {
                executeWithError();
            }catch(BuildException ex){
                log("Caught exception :"+ex.getMessage(),Project.MSG_WARN);
                Throwable ex1 = ex;
                while(ex1!=null) {
                    StackTraceElement[] stack = ex1.getStackTrace();
                    for(int i=0; i<stack.length; ++i) {
                        log(stack[i].getClassName()+"."+stack[i].getMethodName()+" at "+stack[i].getFileName()+","+stack[i].getLineNumber(),Project.MSG_VERBOSE);
                    }
                    ex1 = ex1.getCause();
                    if (ex1!=null) {
                        log("Caused by "+ex1.getMessage(),Project.MSG_VERBOSE);
                    }
                }
            }
       }else{
            executeWithError();
       }
    }


    public void executeWithError() throws BuildException {
     executeInprocess();
    }

      public void executeInprocess() throws BuildException {
        Main main = new Main();
        try {
          //if (!CompileTimeConstants.IN_IDE) {
            try{
              TermWare.getInstance().setTermLoaderClassName(JSOOTermLoader.class.getName());
            }catch(ClassNotFoundException ex){
                throw new BuildException(ex);
            }catch(InstantiationException ex){
                throw new BuildException(ex);
            }catch(IllegalAccessException ex){
                throw new BuildException(ex);
            }
          //}
          main.init(configuration_);
          main.run();
        }catch(JSOOConfigurationException ex){
            throw new BuildException(ex);
        }catch(JSOOProcessingException ex){
            throw new BuildException(ex);
        }
    }

    private Configuration configuration_ = new Configuration();
    private boolean fork_=false;
    private boolean failOnError_=false;


}
